package com.inno.subject.domain.strategy.subject;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.inno.subject.common.enums.IsDeletedFlagEnum;
import com.inno.subject.common.utils.DateUtils;
import com.inno.subject.domain.entity.SubjectAnswerBO;
import com.inno.subject.domain.entity.SubjectInfoBO;
import com.inno.subject.domain.entity.SubjectTypeBO;
import com.inno.subject.infra.basic.entity.PlannerSubjectAnswer;
import com.inno.subject.infra.basic.entity.PlannerSubjectInfo;
import com.inno.subject.infra.basic.service.PlannerSubjectInfoService;

import java.util.List;

/**
 * @author shenzeyang
 */
public interface SubjectHandler {

    Long getTypeId();

    void handle(SubjectInfoBO subjectInfoBO,PlannerSubjectInfo entity);

    SubjectTypeBO querySubjectType();

    /**
     * 通用行为 题目表插入
     */
    default void saveSubject(PlannerSubjectInfoService subjectInfoService, PlannerSubjectInfo entity){
        subjectInfoService.insert(entity);
    }

    default  PlannerSubjectAnswer builderSubjectAnswer(SubjectInfoBO subjectInfoBO){
        List<SubjectAnswerBO> subjectOptions = subjectInfoBO.getSubjectOptions();
        List<SubjectAnswerBO> subjectOptionAnswer = subjectInfoBO.getSubjectOptionAnswer();
        List<String> optionAnswers = subjectOptionAnswer.stream().map(SubjectAnswerBO::getOption).toList();
        PlannerSubjectAnswer plannerSubjectAnswer = new PlannerSubjectAnswer();
        plannerSubjectAnswer.setId(IdUtil.getSnowflakeNextId());
        plannerSubjectAnswer.setAnswerContent(JSON.toJSONString(subjectOptions));
        plannerSubjectAnswer.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode().toString());
        plannerSubjectAnswer.setCreateTime(DateUtils.getNowDate());
        plannerSubjectAnswer.setCreateBy(subjectInfoBO.getCurrentUser());
        plannerSubjectAnswer.setAnswerOptions(JSON.toJSONString(optionAnswers));
        plannerSubjectAnswer.setSubjectId(subjectInfoBO.getId());
        return plannerSubjectAnswer;
    }
}
